package com.xhu.collegestudentrentalsystem.module.manager.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xhu.collegestudentrentalsystem.annocation.LoginRequired;
import com.xhu.collegestudentrentalsystem.module.manager.entity.News;
import com.xhu.collegestudentrentalsystem.module.manager.service.INewsService;
import com.xhu.collegestudentrentalsystem.module.user.entity.Page;
import com.xhu.collegestudentrentalsystem.util.HostHolder;
import com.xhu.collegestudentrentalsystem.util.ResultData;
import com.xhu.collegestudentrentalsystem.util.constant.KeyMsg;
import com.xhu.collegestudentrentalsystem.util.constant.PointMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/27 9:59
 */
@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private INewsService newsService;

    @Autowired
    private HostHolder hostHolder;

    /**
     * 查询新闻列表
     */
    @LoginRequired
    @RequestMapping("/newsList")
    public String newList(Page page, Model model){
        page.setLimit(5);
        List<News> news = newsService.findAllNews(page);
        model.addAttribute(KeyMsg.news.NEWS_LIST, news);
        page.setPath("/news/newsList");
        return "site/news-list";
    }

    /**
     * 转到发布或修改新闻页面
     */
    @LoginRequired
    @RequestMapping("/publishNewsPage")
    public String publishNewsPage(Long newsId, Model model){
        if(newsId != null && newsId != 0){
            // 查询新闻信息
            News news = newsService.findNewsById(newsId);
            model.addAttribute(KeyMsg.news.NEWS, news);
        }
        return "site/news-add";
    }

    /**
     * 发布或修改新闻
     *      查询新闻是否已存在，如果存在，则修改，如果不存在，则新增
     */
    @RequestMapping("/publishNews")
    @ResponseBody
    public ResultData publishNews(News news){
        if(hostHolder.getUser() == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        if(news.getId() != null){
            News originNews = newsService.findNewsById(news.getId());
            if(originNews != null){
                Integer updateResult = newsService.updateNews(news);
                if(updateResult > 0){
                    return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.modify.MODIFY_SUCCESS);
                }else {
                    return new ResultData(ResultData.OPERATION_FAIL, PointMsg.modify.MODIFY_FAIL);
                }
            }
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.news.NOT_FOUND);
        }
        // 新建插入一条新闻
        news.setCreateTime(new Date());
        Integer addResult = newsService.addNews(news);
        if(addResult > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.news.ADD_SUCCESS);
        }else {
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.news.ADD_FAIL);
        }
    }

    /**
     * 删除新闻
     */
    @RequestMapping("/deleteNews")
    @ResponseBody
    public ResultData deleteNews(Long newsId){
        if(hostHolder.getUser() == null){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.UN_LOGIN);
        }
        Integer result = newsService.deleteNewsById(newsId);
        if(result > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.delete.DELETE_SUCCESS);
        }else {
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.delete.DELETE_FAIL);
        }
    }

    /**
     * 新闻资讯页面显示
     */
    @RequestMapping("/allNewsInfoDisplay")
    public String allNewsInfoDisplay(Page page, Model model){
        page.setLimit(5);
        List<News> news = newsService.findAllNews(page);
        model.addAttribute(KeyMsg.news.NEWS_LIST, news);
        page.setPath("/news/allNewsInfoDisplay");
        return "site/news-index-list";
    }

    /**
     * 新闻资讯详情页面
     */
    @RequestMapping("/newsDetail")
    public String newsDetail(Long newsId, Model model){
        News news = newsService.findNewsById(newsId);
        if(news == null){
            return "error/500";
        }
        model.addAttribute(KeyMsg.news.NEWS, news);
        return "site/news-detail";
    }


}
